﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Steganograf
{
    public class Redundancy
    {
        public static int CODE_LENGTH = 5;

        // 1 = 01011, 0 = 10100
        public static byte[] GetRedundantBits(byte pBit)
        {
            List<byte> result = null;
            if (pBit == 0)
                result = new List<byte>() { 1, 0, 1, 0, 0 };
            else
                result = new List<byte>() { 0, 1, 0, 1, 1 };

            return result.ToArray();
        }

        public static byte GetBitFromRedundancy(byte[] pBits)
        {
            string bitsString = "";
            foreach (byte bit in pBits)
                bitsString += bit.ToString();

            if (bitsString == "10100")
                return 0;
            else if (bitsString == "01011")
                return 1;
            else if (CountHummingDistance(bitsString, "10100") < 3)
                return 0;
            else
                return 1;
        }

        private static int CountHummingDistance(string pSource, string pPattern)
        {
            if(pSource.Length != pPattern.Length) return pPattern.Length;
            int differences = 0;
            for (int charIndex = 0; charIndex < pSource.Length; charIndex++)
            {
                if (pSource[charIndex] != pPattern[charIndex])
                    differences++;
            }
            return differences;
        }
    }
}
